﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class EstTest03
    {
        public EstTest03()
        {
            int testCase = Int32.Parse(TestConsole.ReadLine());
            while (testCase-- > 0)
            {
                string[] input = TestConsole.ReadLine().Split(' ');
                int rightCount = Int32.Parse(input[0]);
                int orderCount = Int32.Parse(input[1]);

                bool[] closed = new bool[rightCount + 1];
                bool[] open = new bool[rightCount + 1];
                int[] numbering = new int[rightCount + 1];
                int[] fromCount = new int[rightCount + 1];
                int[] loop = new int[Math.Max(orderCount + 1, rightCount + 1)];
                List<int>[] orderList = new List<int>[rightCount + 1];
                List<int>[] reverseList = new List<int>[Math.Max(orderCount + 1, rightCount + 1)];
                for (int i = 1; i <= rightCount; i++)
                {
                    orderList[i] = new List<int>();
                }
                for (int i = 0; i < reverseList.Length; i++)
                {
                    reverseList[i] = new List<int>();
                }
                for (int i = 0; i < orderCount; i++)
                {
                    input = TestConsole.ReadLine().Split(' ');
                    int from = Int32.Parse(input[0]);
                    int to = Int32.Parse(input[1]);
                    fromCount[to]++;
                    orderList[from].Add(to);
                }
                Stack<int> stack = new Stack<int>();
                
                int number = 0;
                for (int i = 1; i <= rightCount; i++)
                {
                    if (!closed[i])
                    { stack.Push(i); }
                    while (stack.Count > 0)
                    {
                        int now = stack.Peek();
                        open[now] = true;
                        bool findTo = false;
                        for (int j = 0; j < orderList[now].Count; j++)
                        {
                            int to = orderList[now][j];
                            if (!closed[to] && !open[to])
                            {
                                stack.Push(to);
                                findTo = true;
                            }
                        }
                        if (!findTo)
                        {
                            closed[now] = true;
                            stack.Pop();
                            numbering[now] = ++number;
                        }
                    }
                }
                
                for (int i = 1; i < orderList.Length; i++)
                {
                    for (int j = 0; j < orderList[i].Count; j++)
                    {
                        reverseList[numbering[i]].Add(numbering[orderList[i][j]]);
                    }
                    reverseList[numbering[i]].Sort();
                }

                closed = new bool[Math.Max(orderCount + 1, rightCount + 1)];
                open = new bool[Math.Max(orderCount + 1, rightCount + 1)];
                for (int i = 1; i <= rightCount; i++)
                {
                    if (!closed[i])
                    { stack.Push(i); }
                    while (stack.Count > 0)
                    {
                        int now = stack.Peek();
                        if (!open[now])
                        {
                            open[now] = true;
                            loop[now] = i;
                            //Console.Write(numbering_r[now] + " ");
                        }
                        bool findTo = false;
                        for (int j = 0; j < reverseList[now].Count; j++)
                        {
                            int to = reverseList[now][j];
                            if (!closed[to] && !open[to])
                            {
                                stack.Push(to);
                                findTo = true;
                            }
                        }
                        if (!findTo)
                        {
                            closed[now] = true;
                            stack.Pop();
                        }
                    }
                    //Console.Write("\n");
                }
                Dictionary<int, int> loopDic = new Dictionary<int, int>();
                for (int i = 1; i <= rightCount; i++)
                {
                    if (!loopDic.ContainsKey(loop[i]))
                    {
                        loopDic.Add(loop[i], 0);
                    }
                }
                for (int i = 1; i <= rightCount; i++)
                {
                    int now = i;
                    for (int j = 0; j < orderList[i].Count; j++)
                    {
                        loopDic[loop[orderList[i][j]]]++;
                        
                    }
                }
                int result = 0;
                foreach(int i in loopDic.Keys)
                {
                    if (loopDic[i] == 0)
                    {
                        result++;
                    }
                }
                if (result > 0)
                {
                    Console.WriteLine(result);
                }
                else
                {
                    Console.WriteLine(1);
                }


                    TestConsole.ReadLine();
            }
        }
    }
}
